
package ru.surfstudio.webservicetemplate.util;

import ru.surfstudio.webservicetemplate.service.HttpService;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;

/**
 * @author Vladimir Makeev
 */
public class DialogUtil {

    public static Dialog createProgressDialog(final Context context, final int stringId) {
        return createProgressDialog(context, context.getString(stringId));
    }

    public static Dialog createProgressDialog(final Context context, final String dialogText) {
        ProgressDialog dialog = new ProgressDialog(context);
        dialog.setMessage(dialogText);
        dialog.setIndeterminate(true);
        dialog.setCancelable(false);
        return dialog;
    }

    public static Dialog createCancelableProgressDialog(final Context context, final int stringId) {
        return createCancelableProgressDialog(context, context.getString(stringId));
    }

    public static Dialog createCancelableProgressDialog(final Context context,
            final String dialogText) {
        Dialog dialog = DialogUtil.createProgressDialog(context, dialogText);
        dialog.setCancelable(true);
        dialog.setOnCancelListener(new OnCancelListener() {

            @Override
            public void onCancel(DialogInterface dialog) {
                Intent intent = new Intent(context, HttpService.class);
                context.stopService(intent);
            }
        });
        return dialog;
    }
}
